#include "mq_connection.hpp"

void callback(mq::Channel::ptr& channel,const std::string& consumer_id,
            const mq::BasicProperties* bp,const std::string& body)
{
    std::cout << consumer_id << "消费了消息：" << body << std::endl;
    channel->basicAck(bp->id());
}

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        std::cout<<"usage: ./consume_client queue1\n";
        return -1;
    }
    mq::AsyncWorker::ptr worker_ptr=std::make_shared<mq::AsyncWorker>();
    mq::Connection::ptr connection_ptr=std::make_shared<mq::Connection>("127.0.0.1",8080,worker_ptr);
    //1.创建信道
    mq::Channel::ptr channel=connection_ptr->openChannel();
    //2.声明操作
    google::protobuf::Map<std::string,std::string> tmp_map;
    channel->declareExchange("exchange1",mq::ExchangeType::TOPIC,true,false,tmp_map);
    channel->declareQueue("queue1",true,false,false,tmp_map);
    channel->declareQueue("queue2",true,false,false,tmp_map);
    channel->queueBind("exchange1","queue1","queue1");
    channel->queueBind("exchange1","queue2","new.music.#");
    //3.消费回调
    auto cb=std::bind(callback,channel,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3);
    channel->basicConsume("consume1",argv[1],false,cb);
    while(1) std::this_thread::sleep_for(std::chrono::seconds(3));
    connection_ptr->closeChannel(channel);
    return 0;
}